﻿#region Đào Mạnh Hòa
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace GUI.BanBuon
{
    public partial class frmThanhToan : Form
    {
        public static bool kiemtra = false;
        bool checkClose = false;
        bool check = false;
        Entities.HDBanBuon temp;
        public static Entities.HDBanBuon hdbanbuon;
        public static double duno = 0;

        public frmThanhToan()
        {
            InitializeComponent();
        }
        public frmThanhToan(Entities.HDBanBuon hdbanbuon, double tongtien, string hanhdong, double tienchietkhauthuongmai)
        {
            InitializeComponent();
            txtKhachTra.ReadOnly = true;
            btnThanhToan.Enabled = false;

            tongphaitra = double.Parse(hdbanbuon.TongTienThanhToan) - double.Parse(hdbanbuon.GiaTriThe);
            lbTongTien.Text = lblKhachTra.Text = (Common.Utilities.FormatMoney(hdbanbuon.TongTienThanhToan));
            lblChietKhauThuongMai.Text = Common.Utilities.FormatMoney(tienchietkhauthuongmai.ToString());
            lblChietKhauThuongMai.Text = Convert.ToString(tongtien - Convert.ToDouble(hdbanbuon.TongTienThanhToan));
            double abc = Convert.ToDouble(hdbanbuon.ThanhToanNgay);
            if (abc < 0)
                txtKhachTra.Text = "0";
            else
                txtKhachTra.Text = abc.ToString();
            lbTheVip.Text = Common.Utilities.FormatMoney(hdbanbuon.GiaTriThe);
            lblTongTienDaChietKhau.Text = Common.Utilities.FormatMoney((double.Parse(lblChietKhauThuongMai.Text) + double.Parse(lbTongTien.Text)).ToString());
        }

        public frmThanhToan(Entities.HDBanBuon hdbanbuon, double tienchietkhauthuongmai)
        {
            InitializeComponent();
            try
            {
                this.temp = hdbanbuon;
                tongphaitra = double.Parse(hdbanbuon.TongTienThanhToan) - double.Parse(hdbanbuon.GiaTriThe);
                lbTongTien.Text = lblKhachTra.Text = Common.Utilities.FormatMoney(hdbanbuon.TongTienThanhToan);
                lblChietKhauThuongMai.Text = Common.Utilities.FormatMoney(tienchietkhauthuongmai.ToString());
                double abc = (double.Parse(hdbanbuon.TongTienThanhToan) - double.Parse(hdbanbuon.GiaTriThe));
                if (abc < 0)
                    txtKhachTra.Text = "0";
                else
                    txtKhachTra.Text = abc.ToString();
                lbTheVip.Text = Common.Utilities.FormatMoney(hdbanbuon.GiaTriThe);
                lblTongTienDaChietKhau.Text = Common.Utilities.FormatMoney((double.Parse(lblChietKhauThuongMai.Text) + double.Parse(lbTongTien.Text)).ToString());

            }
            catch
            {
            }
        }

        void ThanhToan()
        {
            try
            {
                string khachtra = "0";
                if (txtKhachTra.Text.Length != 0)
                    khachtra = txtKhachTra.Text;
                temp.ThanhToanNgay = khachtra;
                temp.ThanhToanSauKhiLapPhieu = "0";
                if (!Validat())
                {
                    return;
                }
                if (lbDuTra.Text != "0")
                {
                    duno = double.Parse(lbTongTien.Text) - double.Parse(txtKhachTra.Text);
                }
                else
                    duno = 0;
                hdbanbuon = temp;
                checkClose = true;
                this.Close();

            }
            catch
            {
            }
        }

        void Thoat()
        {
            this.Close();
        }

        public double tongphaitra;

        public bool Validat()
        {
            return true;
        }

        private void frmThanhToan_Load(object sender, EventArgs e)
        {
            txtKhachTra.TextChanged += new EventHandler(txtKhachTra_TextChanged);
            btnThanhToan.Click += new EventHandler(btnThanhToan_Click);
            btnThoat.Click += new EventHandler(btnThoat_Click);
            this.FormClosing += new FormClosingEventHandler(frmThanhToan_FormClosing);
        }

        void btnThoat_Click(object sender, EventArgs e)
        {
            Thoat();
        }

        void btnThanhToan_Click(object sender, EventArgs e)
        {
            ThanhToan();
        }

        void frmThanhToan_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (!checkClose)
                if (MessageBox.Show(Common.MSG.MSG2(), Common.MSG.TieuDe, MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes)
                {
                    kiemtra = true;
                    e.Cancel = checkClose;
                }
                else
                    e.Cancel = !checkClose;
        }


        private void txtKhachTra_TextChanged(object sender, EventArgs e)
        {
            try
            {
                double i = double.Parse(txtKhachTra.Text);
                if (i < 0)
                {
                    txtKhachTra.Text = "";
                    lblKhachTra.Text = "0";
                    lbDuTra.Text = "0";
                }
                lblKhachTra.Text = Common.Utilities.FormatMoney(txtKhachTra.Text);
                string dutra = (double.Parse(lblKhachTra.Text) - tongphaitra).ToString();


                lbDuTra.Text = Common.Utilities.FormatMoney(dutra);

            }
            catch
            {
                txtKhachTra.Text = "";
                lblKhachTra.Text = "0";
                string dutra = (0 - tongphaitra).ToString();
                lbDuTra.Text = Common.Utilities.FormatMoney(dutra);
            }
        }
    }
}
#endregion